import pygame.font

from pygame.sprite import Group
from ship import Ship
import json

class Scoredboard():
	"""显示得分信息的类"""
	
	def __init__(self, ai_settings, screen, stats):
		"""初始化显示得分涉及的属性"""
		self.screen = screen
		self.screen_rect = screen.get_rect()
		self.ai_settings = ai_settings
		self.stats = stats
		
		#显示得分信息时使用的字体设置
		self.text_color = (30, 30, 30)
		self.font = pygame.font.SysFont(None, 48)
		
		#准备初始得分图像
		self.prep_score()
		self.prep_high_score()
		self.prep_level()
		self.prep_ships()
		
	def prep_ships(self):
		"""显示还余下多少艘飞船"""
		self.ships = Group()
		for ship_number in range(self.stats.ships_left):
			ship = Ship(self.ai_settings, self.screen)
			ship.rect.x = 10 + ship_number * ship.rect.width
			ship.rect.y = 10
			self.ships.add(ship)
	
	def prep_level(self):
		"""将等级转换为渲染的图像"""
		self.level_image = self.font.render(str(self.stats.level), True, 
			self.text_color, self.ai_settings.bg_color)
		
		#将等级放在得分下方
		self.level_rect = self.level_image.get_rect()
		self.level_rect.right = self.score_rect.right
		self.level_rect.top = self.score_rect.bottom + 10
		
	def prep_high_score(self):
		"""将最高得分转换为渲染的图像"""
		#首先从存储获取记录，没有读取stats的默认数据
		self.high_score = self.stats.high_score
		#更新文件
		self.update_file()
		high_score = int(round(self.high_score, -1))
		high_score_str = "{:,}".format(high_score)
		self.high_score_image = self.font.render(high_score_str, True,
			self.text_color, self.ai_settings.bg_color)
		
		#将最高得分放在屏幕顶部中央
		self.high_score_rect = self.high_score_image.get_rect()
		self.high_score_rect.centerx = self.screen_rect.centerx
		self.high_score_rect.top = self.score_rect.top
	
	def update_file(self):
		filename = "high_score.json"
		try:
			with open(filename) as f_obj:
				number = json.load(f_obj)
				if self.high_score < number:
					self.high_score = number
					self.stats.high_score = number
		except FileNotFoundError:
			self.high_score = 0
		#保存数据
		with open(filename,'w') as f_obj:
			json.dump(self.high_score, f_obj)
	
	def prep_score(self):
		"""将得分转换为一幅渲染的图像"""
		round_score = int(round(self.stats.score,-1))
		score_str = "{:,}".format(round_score)
		#score_str = str(self.stats.score)
		self.score_image = self.font.render(score_str, True, self.text_color,
			self.ai_settings.bg_color)
		
		#将得分放在屏幕右上角
		self.score_rect = self.score_image.get_rect()
		#print(self.screen_rect.right)
		self.score_rect.right = self.screen_rect.right -20
		#self.score_rect.right = 1020
		print(self.score_rect.right)
		self.score_rect.top = 20
	
	def show_score(self):
		"""在屏幕上显示得分和最高得分"""
		self.screen.blit(self.score_image, self.score_rect)
		self.screen.blit(self.high_score_image, self.high_score_rect)
		self.screen.blit(self.level_image, self.level_rect)
		#绘制飞船
		self.ships.draw(self.screen)
